package com.unei.guava.demo.fp;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sunlei on 2015/1/21.
 */
public class FunctionTest {
    @Test
    public void test(){
        Map<String,State> stateMap=new HashMap<String,State>();
        State s=new State("New York");
        s.addCity("new");
        s.addCity("york");
        stateMap.put("NY",s);

        //Functions.forMap 接收一个Map集合作为参数，返回一个Function，用于执行Map集合的查找
        Function<String,State> lookup= Functions.forMap(stateMap);
        Function<State,String> stateFunction=new StateToCityString();

        //Function<A, C> compose(Function<B, C> g, Function<A, ? extends B> f)
        //接收两个Function作为参数，返回两个Function的组合
        //f的输出会作为g的输入，g输出为最终作为compose的输出
        Function<String,String> composed=Functions.compose(stateFunction,lookup);
        String str=composed.apply("NY");
        System.out.println(str);

    }
}
